import { request } from "@/api-services/request";
import config from "@/config/config";
// import { AxiosPromise } from "axios";
import {
  DirectoryContentsReuqest,
  GetAllDiskResult,
  RenameDirectoryContentsRequest,
  DownloadTelegraReuqest,
  DeleteDirectoryContentRequest,
} from "../models";
import { GetDirectoryContentsResultExt } from "../models/extension/extension";

export default {
  GetDirectoryContents(data: DirectoryContentsReuqest) {
    return request.post<GetDirectoryContentsResultExt>(
      "/NasFile/GetDirectoryContents",
      data
    );
  },
  async FileDownload(path, fileName) {
    debugger
    let blob = await request.get(
      `/NasFile/FileDownload`,
      { path, fileName },
      { responseType: "blob" }
    );
    this.DownloadFileByBlob(blob, fileName);
  },
  PlayerVideo(path, fileName) {
    return request.get(
      `/NasFile/PlayerVideo`,
      { path, fileName },
      { responseType: "stream" } //"application/octet-stream"
    );
  },
  FileLink(path) {
    return `${config.apiBaseUrl}/NasFile/FileLink?path=${path}`;
  },
  GetAllDisk() {
    return request.get<GetAllDiskResult[]>(`/NasFile/GetAllDisk`);
  },
  GetAllDiskByUser() {
    return request.get<GetAllDiskResult[]>(`/NasFile/GetAllDiskByUser`);
  },
  GetFolderTree(data: DirectoryContentsReuqest) {
    return request.post("/NasFile/GetFolderTree", data);
  },
  CreateDirectory(data: DirectoryContentsReuqest) {
    return request.post("/NasFile/CreateDirectory", data);
  },
  RenameDirectoryContents(data: RenameDirectoryContentsRequest) {
    return request.post("/NasFile/RenameDirectoryContents", data);
  },
  DeleteDirectoryContent(data: DeleteDirectoryContentRequest) {
    return request.post("/NasFile/DeleteDirectoryContent", data);
  },
  DownloadTelegra(data: DownloadTelegraReuqest) {
    return request.post(`/NasFile/DownloadTelegra`, data);
  },
  DownloadFileByBlob(blob, fileName: string | null | undefined) {
    let blobUrl = window.URL.createObjectURL(blob);
    let link = document.createElement("a");
    link.download = fileName || "defaultName";
    link.style.display = "none";
    link.href = blobUrl;
    // 触发点击
    document.body.appendChild(link);
    link.click();
    // 移除
    document.body.removeChild(link);
  },
};
